﻿Imports System.Data

Partial Class WebForm_ManageTrainingCourse_AddTrainingCourseGroup
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.FV.InsertItemTemplate = Me.FV.ItemTemplate
        Me.FV.EditItemTemplate = Me.FV.ItemTemplate
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

        End If
    End Sub

#Region "Delegate"

    Public Event Saving(ByVal sender As Object, ByVal e As EventArgs)

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Page.IsValid = False Then
            Return
        End If

        If DataAccessMode = FormViewMode.Insert Then
            FV.InsertItem(True)

        ElseIf DataAccessMode = FormViewMode.Edit Then
            FV.UpdateItem(True)

        End If

        RaiseEvent Saving(Me, New EventArgs)

    End Sub

#End Region

    Protected Sub ddlGroupType_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ddlGroupType As DropDownList = TryCast(sender, DropDownList)
        ddlGroupType.Items.Insert(0, New ListItem(Me.GetLocalResourceObject("PleaseSelect"), "0"))
    End Sub

    Protected Sub FV_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FV.ItemInserting
        e.Values("LoginUser") = Page.User.Identity.Name
    End Sub

    Protected Sub ddlGroupType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        SearchTrainingCourseControl1.CourseType = TryCast(sender, DropDownList).SelectedValue

        If TryCast(sender, DropDownList).SelectedValue = "B" Then
            TryCast(FV.FindControl("txtAllHour"), TextBox).Text = MyObj.GetParameter("VBook", "BasicTrainingHour")
        ElseIf TryCast(sender, DropDownList).SelectedValue = "S" Then
            TryCast(FV.FindControl("txtAllHour"), TextBox).Text = MyObj.GetParameter("VBook", "SpecialTrainingHour")
        Else
            TryCast(FV.FindControl("txtAllHour"), TextBox).Text = ""
        End If
    End Sub

    Protected Sub ddlUOrganizationID_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim ddlUOrganizationID As DropDownList = TryCast(sender, DropDownList)

        If ddlUOrganizationID.Items.Count > 1 Then

            ddlUOrganizationID.Items.Insert(0, New ListItem(Me.GetLocalResourceObject("PleaseSelect"), String.Empty))

        End If

    End Sub

    '運用單位 
    Protected Sub ddlUOrganizationID_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim dt As DataTable
        Dim ddlUOrganizationID As DropDownList = TryCast(sender, DropDownList)
        'MyObj.ShowMessage(Me.DataAccessMode.ToString())
        If Me.DataAccessMode = FormViewMode.ReadOnly Then
            Dim sql As New MyStringBuilder
            Dim dat As New clsData

            sql.AppendFormat("select * from organizationview ")
            sql.AppendFormat("where UOrganizationID = '{0}' ", DataBinder.Eval(Me.FV.DataItem, "UOrganizationID"))

            dt = dat.GetData(sql)
        Else
            dt = clsAuth.GetDDLDirectOrganizationData()

        End If

        If ddlUOrganizationID.Items.Count = 0 AndAlso (Not dt Is Nothing) AndAlso (dt.Rows.Count > 0) Then

            For Each objDataRow As DataRow In dt.Rows

                ddlUOrganizationID.Items.Add(New ListItem(objDataRow.Item("OrganizationName").ToString(), objDataRow.Item("UOrganizationID").ToString()))
            Next

            If ddlUOrganizationID.Items.Count > 1 Then
                ddlUOrganizationID.Items.Insert(0, New ListItem(Me.GetLocalResourceObject("PleaseSelect"), String.Empty))
            End If

        End If

    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

        If DataAccessMode = FormViewMode.ReadOnly Then

            btnSave.Visible = False
            btnAddCourse.Visible = False

            GV.Columns(4).Visible = False
            GV.Columns(5).Visible = False
        Else
            btnSave.Visible = True
            btnAddCourse.Visible = True

            GV.Columns(4).Visible = True
            GV.Columns(5).Visible = True
        End If

    End Sub
End Class

Partial Class WebForm_ManageTrainingCourse_AddTrainingCourseGroup

    Public Sub InitData(ByVal strId As String)

        '刪除暫存檔
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("DELETE TrainingCourseGroupMapping WHERE GroupId=0 and CreateBy='{0}';", Page.User.Identity.Name)

        dat.AccData(sql)

        KeyId = strId

        If strId = 0 Then
            '新增
            Me.DataAccessMode = FormViewMode.Insert
            Me.FV.ChangeMode(FormViewMode.Insert)
        Else
            '編輯
            'Me.DataAccessMode = FormViewMode.Edit
            'Me.FV.ChangeMode(FormViewMode.Edit)

            Me.FV.ChangeMode(Me.DataAccessMode)

            ObjectDataSource1.SelectParameters("Id").DefaultValue = KeyId

            FV.DataSourceID = ObjectDataSource1.ID
            FV.DataBind()

        End If

        ObjectDataSource2.SelectParameters("GroupId").DefaultValue = KeyId

        SearchTrainingCourseControl1.GroupId = KeyId
        SearchTrainingCourseControl1.CourseType = 0

    End Sub

#Region "Property"

    Public Property DataAccessMode() As FormViewMode
        Get
            If ViewState("DataAccessMode") Is Nothing Then
                Return FormViewMode.Insert
            End If

            Return ViewState("DataAccessMode")
        End Get
        Set(ByVal value As FormViewMode)
            ViewState("DataAccessMode") = value
        End Set

    End Property

    Public Property KeyId() As String
        Get
            Return ViewState("KeyId").ToString()
        End Get
        Set(ByVal value As String)
            ViewState("KeyId") = value
        End Set

    End Property

#End Region

End Class

Partial Class WebForm_ManageTrainingCourse_AddTrainingCourseGroup

    '開窗選取訓練課程
    Protected Sub btnAddCourse_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddCourse.Click
        Dim groupType As String = TryCast(Me.FV.FindControl("ddlGroupType"), DropDownList).SelectedValue
        SearchTrainingCourseControl1.CourseType = groupType
        SearchTrainingCourseControl1.InitDataAndShow()
    End Sub

    '選完志工儲存所選志工
    Protected Sub SearchTrainingCourseControl1_OK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchTrainingCourseControl1.OK_Click
        Me.SaveTrainingCourse()
        GV.DataBind()
    End Sub

    '儲存所選志工
    Private Function SaveTrainingCourse() As Boolean

        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        sql.Length = 0

        Using scope As New Transactions.TransactionScope

            For Each TrainingCourseID As String In Me.SearchTrainingCourseControl1.SelectedValues

                sql.AppendFormat("INSERT TrainingCourseGroupMapping (GroupId,CourseId,CreateBy,CreateDate,TeacherName) select ")
                sql.AppendFormat("  '{0}' ", KeyId)
                sql.AppendFormat(" ,'{0}' ", TrainingCourseID)
                sql.AppendFormat(" ,'{0}' ", Page.User.Identity.Name)
                sql.AppendFormat(" ,getdate() ")
                sql.AppendFormat(" ,{0} ", "TeacherName")
                sql.AppendFormat("from TrainingCourse ")
                sql.AppendFormat("where ID = {0} ", TrainingCourseID)

            Next

            If sql.Length <> 0 Then
                dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)
            End If

            scope.Complete()

        End Using

        Return True

    End Function

#Region "DataBinding-時分拆開"
    Protected Sub txtStartHour_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim txtStartHour As TextBox = sender
        Dim gvr As GridViewRow = txtStartHour.NamingContainer

        Dim strStartTime As String = DataBinder.Eval(gvr.DataItem, "StartTime").ToString()

        If strStartTime.Trim() <> String.Empty Then
            TryCast(sender, TextBox).Text = DataBinder.Eval(gvr.DataItem, "StartTime").ToString().Split(":")(0)
        End If
    End Sub

    Protected Sub txtStartMinute_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim txtStartMinute As TextBox = sender
        Dim gvr As GridViewRow = txtStartMinute.NamingContainer

        Dim strStartTime As String = DataBinder.Eval(gvr.DataItem, "StartTime").ToString()

        If strStartTime.Trim() <> String.Empty Then
            TryCast(sender, TextBox).Text = DataBinder.Eval(gvr.DataItem, "StartTime").ToString().Split(":")(1)
        End If
    End Sub

    Protected Sub txtEndHour_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim txtEndHour As TextBox = sender
        Dim gvr As GridViewRow = txtEndHour.NamingContainer

        Dim strEndTime As String = DataBinder.Eval(gvr.DataItem, "EndTime").ToString()

        If strEndTime.Trim() <> String.Empty Then
            TryCast(sender, TextBox).Text = DataBinder.Eval(gvr.DataItem, "EndTime").ToString().Split(":")(0)
        End If
    End Sub

    Protected Sub txtEndMinute_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim txtEndMinute As TextBox = sender
        Dim gvr As GridViewRow = txtEndMinute.NamingContainer

        Dim strEndTime As String = DataBinder.Eval(gvr.DataItem, "EndTime").ToString()

        If strEndTime.Trim() <> String.Empty Then
            TryCast(sender, TextBox).Text = DataBinder.Eval(gvr.DataItem, "EndTime").ToString().Split(":")(1)
        End If
    End Sub

#End Region

#Region "RowUpdating-時分合併"

    Protected Sub GV_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GV.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            If Me.DataAccessMode = FormViewMode.ReadOnly Then
                e.Row.FindControl("lbtnDelete").Visible = False
            End If

        End If
    End Sub
    Protected Sub GV_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GV.RowUpdating

        e.NewValues("TeacherName") = TryCast(Me.GV.Rows(e.RowIndex).FindControl("txtTeacherName"), TextBox).Text
        e.NewValues("StartTime") = TryCast(GV.Rows.Item(e.RowIndex).FindControl("txtStartHour"), TextBox).Text & ":" & TryCast(GV.Rows.Item(e.RowIndex).FindControl("txtStartMinute"), TextBox).Text
        e.NewValues("EndTime") = TryCast(GV.Rows.Item(e.RowIndex).FindControl("txtEndHour"), TextBox).Text & ":" & TryCast(GV.Rows.Item(e.RowIndex).FindControl("txtEndMinute"), TextBox).Text
        e.NewValues("UpdateBy") = Page.User.Identity.Name

    End Sub
#End Region

End Class
